package com.globant.scrollbar;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private static final int NOTIFICATION_ID = 0;
	
	private ProgressBar progressBar;
	private TextView progress;
	private Button executeButton;
	
	private void initialState() {
		progressBar = (ProgressBar)findViewById(R.id.progressBar);
        progressBar.setVisibility(ProgressBar.INVISIBLE);
        progressBar.setProgress(0);
        
        executeButton = (Button)findViewById(R.id.executeButton);
        executeButton.setEnabled(Boolean.TRUE);
        
        progress = (TextView)findViewById(R.id.progress);
        progress.setVisibility(ProgressBar.INVISIBLE);
        progress.setText("0%");
	}
	
	private void finalState() {
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        progressBar.setVisibility(ProgressBar.VISIBLE);
 
        executeButton = (Button)findViewById(R.id.executeButton);
        executeButton.setEnabled(Boolean.FALSE);
        
        progress = (TextView)findViewById(R.id.progress);
        progress.setVisibility(ProgressBar.VISIBLE);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                
        if(getIntent() != null && getIntent().getExtras() != null 
        		&& getIntent().getExtras().containsKey(BusinessSettings.PROGRESS)) {
        	NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        	notificationManager.cancel(NOTIFICATION_ID);
        	
        	this.finalState();
        	int progress = getIntent().getExtras().getInt(BusinessSettings.PROGRESS);
            progressBar.setProgress(progress);
        } else {
        	this.initialState();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void startScrollBar(View view) {
    	
    	this.finalState();
    		
        Map<String, Activity> map = new HashMap<String, Activity>(); 
        WeakReference<Map<String, Activity>> activityMap = new WeakReference<Map<String, Activity>>(map);
        
        map.put(BusinessSettings.MAIN_ACTIVITY, this);
        
        AsyncProcess loadingProgressBar = new AsyncProcess(activityMap);
        loadingProgressBar.execute();
           
    }
    
	public void generateToastMessage() {
		Context context = getApplicationContext();
        CharSequence text = getResources().getString(R.string.process_finished);
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
	}

	
	public void triggerNotification() {
		CharSequence title = getResources().getString(R.string.atention);
		CharSequence message = getResources().getString(R.string.process_finished);

		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(
				R.drawable.ic_dialog_green_flag, getResources().getString(R.string.new_message),
				System.currentTimeMillis());

		Intent notificationIntent = new Intent(this, MainActivity.class);
		
		notificationIntent.putExtra(BusinessSettings.PROGRESS, progressBar.getProgress());
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
				notificationIntent, 0);

		notification.setLatestEventInfo(MainActivity.this, title, message,
				pendingIntent);
		notificationManager.notify(NOTIFICATION_ID, notification);
	}
		
	public ProgressBar getProgressBar() {
		return progressBar;
	}
	
	public void setProgressBar(ProgressBar progressBar) {
		this.progressBar = progressBar;
	}

	public TextView getProgress() {
		return progress;
	}

	public void setProgress(TextView progress) {
		this.progress = progress;
	}
}
